package com.itheima.web;


import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.itheima.service.OrderService;
import com.itheima.support.Result;
import com.itheima.utils.PayUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;


@RestController
public class PayController {
    @Resource
    private OrderService orderService;

//    @Resource
//    private RabbitTemplate rabbitTemplate;

    @GetMapping("/payer/{id}")
    public String payer(@PathVariable("id") String id) {
        return PayUtils.createOrder("6d62ff8c-66d3-43e1-8009" + id, 1);
    }

    @RequestMapping("/payNotify")
    public void payNotify(HttpServletRequest request) throws IOException {
        // 1.接收请求参数（xml）
        ServletInputStream in = request.getInputStream();
        // 2.将xml转为java对象
        XmlMapper xmlMapper = new XmlMapper();
        Map param = xmlMapper.readValue(in, Map.class);
        orderService.updateByOrderId(param);
    }

    @RequestMapping("/selStatus/{id}")
    public Result selStatus(@PathVariable("id") String id) {
      //  System.out.println("id01" + id);
        final Result result = new Result(orderService.selStatus(id));
        //System.out.println("result01"+orderService.selStatus(id));
        return result;
    }

}
